package com.glasses.business.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.sdk.android.oss.model.OSSException;
import com.glasses.business.R;
import com.glasses.business.base.AppBaseFragment;
import com.glasses.business.utils.AliUpLoadCallBack;
import com.glasses.business.utils.AliyunUploadUtils;
import com.glasses.business.utils.Tools;
import com.pizidea.imagepicker.AndroidImagePicker;
import com.pizidea.imagepicker.bean.ImageItem;
import com.pizidea.imagepicker.image.ImagesGridActivity;

import net.neiquan.applibrary.wight.AlertChooser;

import org.haitao.common.utils.AppLog;
import org.haitao.common.utils.BitmapUtis;
import org.haitao.common.utils.CameraUtils;
import org.haitao.common.utils.ToastUtil;

import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

/**
 * Author：邓勇军
 * English Name：Deng,The Sky Fucker!
 * Email：dandycoder@126.com
 * Date：2017/3/7
 * Version：1.0
 */

public class ChildMatchGlassFragment extends AppBaseFragment {
    @InjectView(R.id.glassUpLight)
    LinearLayout glassUpLight;
    @InjectView(R.id.glassHandLight)
    TextView glassHandLight;
    @InjectView(R.id.glassChangeLight)
    TextView glassChangeLight;
    @InjectView(R.id.glassFrame)
    RelativeLayout glassFrame;
    @InjectView(R.id.glassFrameOld)
    RelativeLayout glassFrameOld;
    @InjectView(R.id.glassPair)
    RelativeLayout glassPair;
    @InjectView(R.id.glassName)
    TextView glassName;
    @InjectView(R.id.glassPhone)
    TextView glassPhone;
    @InjectView(R.id.glassAddr)
    TextView glassAddr;
    @InjectView(R.id.glassAddrLL)
    LinearLayout glassAddrLL;
    @InjectView(R.id.glassMoney)
    TextView glassMoney;
    @InjectView(R.id.glassTime)
    TextView glassTime;
    @InjectView(R.id.glassBuy)
    TextView glassBuy;

    @Override
    public int getRootViewId() {
        return R.layout.fragment_child_match_glass;
    }

    @Override
    public void setViews() {
        setTitleTv("配镜");
    }

    @Override
    public void initData() {

    }

    @Override
    public boolean mainTabShow() {
        return false;
    }


    @OnClick({R.id.glassUpLight, R.id.glassHandLight, R.id.glassChangeLight, R.id.glassFrame,R.id.glassFrameOld, R.id.glassPair, R.id.glassAddrLL, R.id.glassBuy})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.glassUpLight:
                showAlertChooser();
                break;
            case R.id.glassHandLight:
                open(new HandLightFragment());
                break;
            case R.id.glassChangeLight:
                open(new LightOrderrFragment());
                break;
            case R.id.glassFrame:
                open(new GlassFrameFragment());
                break;
            case R.id.glassFrameOld:
                open(new OldFrameFragment());
                break;
            case R.id.glassPair:
                open(new GlassPairFragment());
                break;
            case R.id.glassAddrLL:
                open(new AddrFragment());
                break;
            case R.id.glassBuy:
                open(new PayFragment());
                break;
        }
    }

    private void showAlertChooser() {
        new AlertChooser.Builder(getActivity()).setTitle("选择图片").addItem("相机", new AlertChooser.OnItemClickListener() {
            @Override
            public void OnItemClick(AlertChooser chooser) {
                AppLog.e("=================相机==================");
                chooser.dismiss();
                CameraUtils.takePhoto(getActivity());
            }
        }).addItem("相册", new AlertChooser.OnItemClickListener() {
            @Override
            public void OnItemClick(AlertChooser chooser) {
                AndroidImagePicker.getInstance().setSelectMode(AndroidImagePicker.Select_Mode.MODE_SINGLE);
                //AndroidImagePicker.Select_Mode.MODE_SINGLE单选模式
                //AndroidImagePicker.Select_Mode.MODE_MULTI多选模式
                //AndroidImagePicker.getInstance().setSelectLimit(MAX_NUM);//多选模式下  最多选多少张
                AndroidImagePicker.getInstance().setShouldShowCamera(false);//相册里面是否显示拍照按钮
                Intent intent = new Intent(getActivity(), ImagesGridActivity.class);
                startActivityForResult(intent, CameraUtils.LOCAL_PICTURE);
                chooser.dismiss();
            }
        }).setNegativeItem("取消", new AlertChooser.OnItemClickListener() {
            @Override
            public void OnItemClick(AlertChooser chooser) {
                chooser.dismiss();
            }
        }).show();
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == CameraUtils.LOCAL_PICTURE) {
            List<ImageItem> imageList = AndroidImagePicker.getInstance().getSelectedImages();
            if (imageList != null && imageList.size() > 0) {
                initAliyun(imageList.get(0).getPath());
//                mImageView.setImageURI("file://" + imageList.get(0).getPath());
            }
        } else if (requestCode == CameraUtils.TAKE_PICTURE) {
            initAliyun(CameraUtils.photoPath);
//            mImageView.setImageURI("file://" + CameraUtils.photoPath);
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    private void initAliyun(final String path) {
//        Tools.showWaitDialog(getContext(), "请稍后...");
        BitmapUtis.compress(path, 120, 160, new BitmapUtis.CompressCallback() {

            @Override
            public void onsucces(String s) {
                AliyunUploadUtils.getInstance(getActivity()).uploadPhoto(s, new AliUpLoadCallBack() {

                    @Override
                    public void onSuccess(String RootPath, String bucket, String fileName) {
//                        imgPath = RootPath + fileName;
//                        mImageView.setImageURI(imgPath);
//                        NetUtils.getInstance().updateHead(imgPath, new NetCallBack() {
//                            @Override
//                            public void onSuccess(String response, String mssage, ResultModel model) {
//                                Tools.dismissWaitDialog();
//                                ToastUtil.shortShowToast("头像上传成功!");
//                                EventBus.getDefault().post(new BussEvent(BussEvent.USER_INFO_REFUSH));
//                            }
//
//                            @Override
//                            public void onFail(boolean dataError, int errorCode, String mssage) {
//                                Tools.dismissWaitDialog();
//                                ToastUtil.shortShowToast("头像上传失败!");
//                            }
//                        }, null);
                    }

                    @Override
                    public void onFailure(String objectKey, OSSException ossException) {
                        Tools.dismissWaitDialog();
                        ToastUtil.shortShowToast("上传图片到服务器失败...请重新尝试");
                    }
                });
            }

            @Override
            public void onfail() {
                Tools.dismissWaitDialog();
                ToastUtil.shortShowToast("图片太大或者已损坏，请重新选择图片");
            }
        });
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        ButterKnife.inject(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.reset(this);
    }

}
